package com.mrdeer.iot.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mrdeer.common.JSONResult;
import com.mrdeer.iot.CommandExecutor;
import com.mrdeer.iot.bean.command.Instruction;
import com.mrdeer.iot.constant.FrameConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 消息指令相关-整机命令
 *
 * @Author: xie jianchu
 */
@Slf4j
@RestController
@Api(tags = "整机设备指令相关接口")
@RequestMapping("/mrdeer/command/all")
@Transactional
public class CommandAllController {

    @Resource
    private CommandExecutor commandExecutor;

    //1.实时获取设备状态（在收到心跳指令的时候通过websocket回传页面）
    //2.整机指令 1、主动查询 2、静音开启/关闭 3、语音播报控制
    //3.洗涤仓命令 1、授权/解除授权  2、洗涤仓启动/暂停  3、洗涤仓门开关  4、程序设定  5、终止当前运行状态,回到待机状态
    //4.烘干仓指令 1、授权/解除授权  2、烘干仓启动/暂停 3、程序设定  4、烘干仓终止当前运行状态，回到待机状态
    @PostMapping("/queryStatus/{address}")
    @ApiOperation(value = "根据设备地址主动发送查询指令")
    public JSONResult queryDeviceStatus(@PathVariable String address) throws JsonProcessingException {
        commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.complete_machine.search);
        return JSONResult.ok();
    }

    @PostMapping("/set/mute/{address}/{flag}")
    @ApiOperation(value = "根据设备地址设置静音开关 0-静音关闭 1-静音开启")
    public JSONResult setDeviceMute(@PathVariable String address, @PathVariable boolean flag) throws JsonProcessingException {
        commandExecutor.processCommand(address, FrameConst.control, flag ? Instruction.ControlFrame.complete_machine.mute_open : Instruction.ControlFrame.complete_machine.mute_close);
        return JSONResult.ok();
    }

    @PostMapping("/speech/{address}/{type}")
    @ApiOperation(value = "根据设备地址播放语音播报,type为播放的内容,未定")
    public JSONResult speech(@PathVariable String address, @PathVariable String type) throws JsonProcessingException {
        int speechType = Instruction.ControlFrame.complete_machine.speech1;
        switch (type) {
            case "0":
                speechType = Instruction.ControlFrame.complete_machine.speech1;
                break;
            case "1":
                speechType = Instruction.ControlFrame.complete_machine.speech2;
                break;
            case "2":
                speechType = Instruction.ControlFrame.complete_machine.speech3;
                break;
            case "3":
                speechType = Instruction.ControlFrame.complete_machine.speech4;
                break;
            default:
                break;
        }
        commandExecutor.processCommand(address, FrameConst.control, speechType);
        return JSONResult.ok();
    }
}
